{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "confirmed-broadway",
   "metadata": {},
   "source": [
    "# Swiss Einzahlungsschein MIT Referenznummers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "christian-dominant",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fatty-winning",
   "metadata": {},
   "source": [
    "The function `clean_ch_esr()` cleans a column containing Swiss EinzahlungsSchein mit Referenznummer (ESR) strings, and standardizes them in a given format. The function `validate_ch_esr()` validates either a single ESR strings, a column of ESR strings or a DataFrame of ESR strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fatty-position",
   "metadata": {},
   "source": [
    "ESR strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"1878583\"\n",
    "* `standard`: ESR strings with proper whitespace in the proper places, like \"00 00000 00000 00000 00018 78583\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_ch_esr()` and `validate_ch_esr()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "resident-industry",
   "metadata": {},
   "source": [
    "### An example dataset containing ESR strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "elementary-privilege",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"esr\": [\n",
    "            \"18 78583\",\n",
    "            \"210000000003139471430009016\",\n",
    "            \"51824753556\",\n",
    "            \"51 824 753 556\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "isolated-bikini",
   "metadata": {},
   "source": [
    "## 1. Default `clean_ch_esr`\n",
    "\n",
    "By default, `clean_ch_esr` will clean esr strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fiscal-modem",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_ch_esr\n",
    "clean_ch_esr(df, column = \"esr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "alien-gossip",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "published-transcript",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "express-primary",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "every-emission",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ch_esr(df, column = \"esr\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "meaning-classification",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "responsible-register",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ch_esr(df, column = \"esr\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "opposite-maintenance",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned ESR strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "elect-infrared",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ch_esr(df, column=\"esr\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "weighted-junior",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "daily-startup",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "smoking-eating",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ch_esr(df, \"esr\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hindu-jersey",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "laughing-steel",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_ch_esr(df, \"esr\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "expired-helmet",
   "metadata": {},
   "source": [
    "## 4. `validate_ch_esr()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "copyrighted-abraham",
   "metadata": {},
   "source": [
    "`validate_ch_esr()` returns `True` when the input is a valid ESR. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_ch_esr()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_ch_esr()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_ch_esr()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "martial-genetics",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_ch_esr\n",
    "print(validate_ch_esr(\"18 78583\"))\n",
    "print(validate_ch_esr(\"210000000003139471430009016\"))\n",
    "print(validate_ch_esr(\"51824753556\"))\n",
    "print(validate_ch_esr(\"51 824 753 556\"))\n",
    "print(validate_ch_esr(\"hello\"))\n",
    "print(validate_ch_esr(np.nan))\n",
    "print(validate_ch_esr(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "universal-mortality",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "imperial-radius",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_ch_esr(df[\"esr\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ranging-privacy",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "intended-wilderness",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_ch_esr(df, column=\"esr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "tired-taxation",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "atomic-valley",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_ch_esr(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "pointed-wilson",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
